package com.news.yp.baselibrary.navigation;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.news.yp.baselibrary.R;

public abstract class AbsNavigationBar<P extends AbsNavigationBar.Builder.AbsNavigationBarParams> implements INavigationBar {
    private P mParams;
    private View mNaviagtionView;

    public AbsNavigationBar(P mparams) {
        this.mParams = mparams;
        createAndBindView();
    }

    public P getmParams() {
        return mParams;
    }

    /**
     * 设置文本
     */
    protected void setText(int viewId, String text) {
        TextView textView = findViewById(viewId);
        textView.setText(text);
    }

    /**
     * 设置点击
     */
    protected void setOnClickListener(int viewId, View.OnClickListener listener) {
        findViewById(viewId).setOnClickListener(listener);
    }

    public <T extends View> T findViewById(int viewId) {
        return (T) mNaviagtionView.findViewById(viewId);
    }

    private void createAndBindView() {
        //创建View
        if (mParams.mParent == null) {
            ViewGroup activityRoot = (ViewGroup) ((Activity) (mParams.mContext)).getWindow().getDecorView().findViewById(android.R.id.content);
            mParams.mParent = (ViewGroup) activityRoot.getChildAt(0);
            Log.e("TAG", "获取到的第一个元素是什么鬼：" + mParams.mParent);
        }
        if (mParams.mParent == null) {
            //获取decorView的title失败，
            return;
        }
        //谁继承了AbsNavagationBar，谁就要重写bindlayoutId(),返回一个layout，即自定义的头部布局
        mNaviagtionView = LayoutInflater.from(mParams.mContext).inflate(bindlayoutId(), mParams.mParent, false);
        //2.添加   这边添加的时候要是不指定0这个位置，要被加在末尾去
        mParams.mParent.addView(mNaviagtionView, 0);

        applyView();

    }


    //使用builder设计模式
    public abstract static class Builder {


        public abstract AbsNavigationBar builder();

        public abstract static class AbsNavigationBarParams {
            public Context mContext;
            public ViewGroup mParent;


            public AbsNavigationBarParams(Context context, ViewGroup mParent) {
                this.mContext = context;
                this.mParent = mParent;
            }

        }
    }
}
